package org.ll.demo25;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Controller
@RequestMapping("/request")
public class RequestController {
    @ResponseBody
    @RequestMapping("getCookies")
    public String getCookies(HttpServletRequest request ){
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length == 0) {
            return "请求中未携带任何 Cookie";
        }
        for(Cookie cookie : cookies){
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
        }
        return "成功获得cookies";
    }

    @RequestMapping("returnHtml")
    public Object index(){
        return "/index.html";
    }
    @RequestMapping("/returnJson")
    @ResponseBody
    public HashMap<String, String> returnJson() {
        HashMap<String, String> map = new HashMap<>();
        map.put("Java", "Java Value");
        map.put("MySQL", "MySQL Value");
        map.put("Redis", "Redis Value");
        return map;
    }
}
